function saveTinMoi(formid, tableId){
    //alert("vo ham");
    var bDataOK = validate(document.getElementById(formid));

    if (bDataOK) {
        var type = document.getElementById('typeOfSaveClick').value;
        if ('New' == type) {
            //alert("Du lieu ok!");
            //var tempStr = document.getElementById('dsTinDaLuuXML').value;
            var tempTable = document.getElementById(tableId);
            //var maxRow = tempTable.rows.length;
            var maxRow = document.getElementById("partOfKeyTinSaveID").value;
            var tempStr = taoXMLStr(formid, maxRow);

            var newRow = tempTable.insertRow(tempTable.rows.length);
            newRow.id = 'tinmoiluucell' + maxRow;
            //alert(newRow.id);
            var newCell = newRow.insertCell(newRow.cells.length);

            var tempTitle = document.getElementById(formid).tieude.value;
            if (tempTitle.length > 15) {
                tempTitle = tempTitle.substring(0, 15);
                tempTitle += '....';
            }
            //tempTitle += "<input type='hidden' id='stttindasave'" + maxRow + "'/>";
            //alert(tempTitle);
            newCell.innerHTML = tempTitle;

            newCell = newRow.insertCell(newRow.cells.length);
            var tempStringTaoNut = "<input type='button' value='Sửa' onclick=\"suaTinDaLuu('"+ formid + "', '"+ newRow.id +"')\"/>";
            tempStringTaoNut += "<input type='button' value='Xóa' onclick=\"xoaTinDaLuu('"+ newRow.id +"')\"/>";
            //alert(tempStringTaoNut);
            newCell.innerHTML = tempStringTaoNut;

            newCell = newRow.insertCell(newRow.cells.length);
            newCell.innerText = tempStr;
            newCell.style.display = 'none';

            document.getElementById("partOfKeyTinSaveID").value = Number(maxRow) + 1;
            var parentDIV = document.getElementById('dsTinMoiTaoDiv');
            parentDIV.style.display = 'block';

            

        //        var testCell = document.getElementById('tinmoiluucell' + maxRow);
        //        alert(testCell.id + "  " + testCell.value + "  " + testCell.innerText);
        } else if ('Update' == type) {
            var rowUpID = document.getElementById('keyTinDaSaveDangDuocUpdate').value;
            var tempRow = document.getElementById(rowUpID);
            if (tempRow != null) {
                var tempCell = tempRow.cells.item(2);
                //alert(formid + "--" +tempCell.innerText);
                var tmpStr = taoXMLStr(formid, ' ');
                tempCell.innerText = tmpStr;
            } else {
                alert ("Bạn đã xóa tin đang cập nhật!\nMột tin đăng khác sẽ được tạo để thay thế!");
                document.getElementById('typeOfSaveClick').value = 'New';
                saveTinMoi(formid, tableId);
            }
            document.getElementById('typeOfSaveClick').value = 'New';
        }
        document.getElementById(formid).btnReset.click();
        setTextForDropDownList(document.getElementById('ddlTinhThanh'),'Hà Nội');
        setTextForDropDownList(document.getElementById('ddlQuanHuyen'),'Hoàn Kiếm');
    }
}

function setTextForDropDownList(ddl, str){
    for (var i=0; i < ddl.options.length;i++) {
        if ( ddl.options[i].text == str ) {
            ddl.options[i].selected = true;
            return;
        }
    }
}

function suaTinDaLuu(formid, rowID){
    document.getElementById('typeOfSaveClick').value = 'Update';
    document.getElementById('keyTinDaSaveDangDuocUpdate').value = rowID;
    //alert('haha');
    var tempCell = document.getElementById(rowID).cells.item(2);
    //alert(formid + "--" +tempCell.innerText);
    var tmpStr = tempCell.innerText;

    var tinDaLuuDoc = stringtoXML(tmpStr);
    var nodeTemp = tinDaLuuDoc.childNodes.item(0);
    //alert(nodeTemp.childNodes.item(0).nodeName);
    var tempstr;
    var form = document.getElementById(formid);
    for (var j=0;j<nodeTemp.childNodes.length;j++){
        if (nodeTemp.childNodes.item(j).nodeName == 'matindang'){
        
        } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloainha'){
            tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
            setTextForDropDownList(form.loainha, tempstr);
        } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloaigiayto'){
            tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
            setTextForDropDownList(form.tenloaigiayto, tempstr);
        } else if (nodeTemp.childNodes.item(j).nodeName == 'cautruc'){
            //alert(nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue);
            form.cautruc.value = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
        } else if (nodeTemp.childNodes.item(j).nodeName == 'thongtinchitiet'){
            form.thongtinchitiet.value = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
        } else if (nodeTemp.childNodes.item(j).nodeName == 'dientichkhuonvien'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdkhuonvienTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvddientichkhuonvien, dvdkhuonvienTemp);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieukhuonvienTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieudientichkhuonvien.value= solieukhuonvienTemp;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'chieungangkhuonvien'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdchieungangkhuonvien = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvdchieungangkhuonvien, dvdchieungangkhuonvien);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieuchieungangkhuonvien = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieuchieungangkhuonvien.value= solieuchieungangkhuonvien;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'chieudaikhuonvien'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdchieudaikhuonvien = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvdchieudaikhuonvien, dvdchieudaikhuonvien);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieuchieudaikhuonvien = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieuchieudaikhuonvien.value= solieuchieudaikhuonvien;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'dientichxaydung'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvddientichxaydung = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvddientichxaydung, dvddientichxaydung);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieudientichxaydung = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieudientichxaydung.value= solieudientichxaydung;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'chieungangxaydung'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdchieungangxaydung = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvdchieungangxaydung, dvdchieungangxaydung);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieuchieungangxaydung = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieuchieungangxaydung.value= solieuchieungangxaydung;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'chieudaixaydung'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdchieudaixaydung = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvdchieudaixaydung, dvdchieudaixaydung);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var solieuchieudaixaydung = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieuchieudaixaydung.value= solieuchieudaixaydung;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'diachi'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                form.diachi.value=tempstr;
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'khuvuc'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var quanhuyen = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.quanhuyen, quanhuyen);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var tinhthanh = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.tinhthanh, tinhthanh);
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'giatien'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                if(nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0) != null) {
                    var dvdgiatienTemp = nodeTemp.childNodes.item(j).childNodes.item(0).childNodes.item(0).nodeValue;
                    setTextForDropDownList(form.dvdgiatien, dvdgiatienTemp);
                }
                if(nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0) != null) {
                    var giatienTemp = nodeTemp.childNodes.item(j).childNodes.item(1).childNodes.item(0).nodeValue;
                    form.solieugiatien.value = giatienTemp;
                }
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'hinhanh'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                form.theFile.src=tempstr;
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'tieudetin'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                form.tieude.value=tempstr;
            }
        } else if (nodeTemp.childNodes.item(j).nodeName == 'tenloaitin'){
            if(nodeTemp.childNodes.item(j).childNodes.item(0) != null) {
                tempstr = nodeTemp.childNodes.item(j).childNodes.item(0).nodeValue;
                setTextForDropDownList(form.loaitin, tempstr);
            //alert(tempstr);
            }
        }
    }
}

function xoaTinDaLuu(rowID){
    var deleteRow = document.getElementById(rowID);
    var parentTable = deleteRow.parentNode.parentNode;
    deleteRow.parentNode.removeChild(deleteRow);
    if (parentTable.rows.length == 1) {
        var parentDIV = document.getElementById('dsTinMoiTaoDiv');
        parentDIV.style.display = 'none';
    }
    
}

function taoXMLStr(formid, rowIndex){
    var form = document.getElementById(formid);

    var str = '<tindang status="new">';

    str += '<matindang>';
    str += rowIndex;
    str += '</matindang>';
    
    str += '<tenloainha>';
    str += form.loainha.options[form.loainha.selectedIndex].text;
    str += '</tenloainha>';

    str += '<tenloaigiayto>';
    str += form.tenloaigiayto.options[form.tenloaigiayto.selectedIndex].text;
    str += '</tenloaigiayto>';

    str += '<cautruc>';
    str += form.cautruc.value;
    str += '</cautruc>';

    str += '<thongtinchitiet>';
    str += form.thongtinchitiet.value;
    str += '</thongtinchitiet>';

    str += '<dientichkhuonvien>';
    str += '<dvddientichkhuonvien>';
    str += form.dvddientichkhuonvien.options[form.dvddientichkhuonvien.selectedIndex].text;
    str += '</dvddientichkhuonvien>';
    str += '<solieu>';
    str += form.solieudientichkhuonvien.value;
    str += '</solieu>';
    str += '</dientichkhuonvien>';

    str += '<chieungangkhuonvien>';
    str += '<dvdchieungangkhuonvien>';
    str += form.dvdchieungangkhuonvien.options[form.dvdchieungangkhuonvien.selectedIndex].text;
    str += '</dvdchieungangkhuonvien>';
    str += '<solieu>';
    str += form.solieuchieungangkhuonvien.value;
    str += '</solieu>';
    str += '</chieungangkhuonvien>';

    str += '<chieudaikhuonvien>';
    str += '<dvdchieudaikhuonvien>';
    str += form.dvdchieudaikhuonvien.options[form.dvdchieudaikhuonvien.selectedIndex].text;
    str += '</dvdchieudaikhuonvien>';
    str += '<solieu>';
    str += form.solieuchieudaikhuonvien.value;
    str += '</solieu>';
    str += '</chieudaikhuonvien>';

    str += '<dientichxaydung>';
    str += '<dvddientichxaydung>';
    str += form.dvddientichxaydung.options[form.dvddientichxaydung.selectedIndex].text;
    str += '</dvddientichxaydung>';
    str += '<solieu>';
    str += form.solieudientichxaydung.value;
    str += '</solieu>';
    str += '</dientichxaydung>';

    str += '<chieungangxaydung>';
    str += '<dvdchieungangxaydung>';
    str += form.dvdchieungangxaydung.options[form.dvdchieungangxaydung.selectedIndex].text;
    str += '</dvdchieungangxaydung>';
    str += '<solieu>';
    str += form.solieuchieungangxaydung.value;
    str += '</solieu>';
    str += '</chieungangxaydung>';

    str += '<chieudaixaydung>';
    str += '<dvdchieudaixaydung>';
    str += form.dvdchieudaixaydung.options[form.dvdchieudaixaydung.selectedIndex].text;
    str += '</dvdchieudaixaydung>';
    str += '<solieu>';
    str += form.solieuchieudaixaydung.value;
    str += '</solieu>';
    str += '</chieudaixaydung>';

    str += '<diachi>';
    str += form.diachi.value;
    str += '</diachi>';

    str += '<khuvuc>';
    str += '<quanhuyen>';
    str += form.quanhuyen.options[form.quanhuyen.selectedIndex].text;
    str += '</quanhuyen>';
    str += '<tinhthanh>';
    str += form.tinhthanh.options[form.tinhthanh.selectedIndex].text;
    str += '</tinhthanh>';
    str += '</khuvuc>';

    str += '<giatien>';
    str += '<dvdgiatien>';
    str += form.dvdgiatien.options[form.dvdgiatien.selectedIndex].text;
    str += '</dvdgiatien>';
    str += '<solieu>';
    str += form.solieugiatien.value;
    str += '</solieu>';
    str += '</giatien>';

    str += '<hinhanh>';
    str += form.theFile.value;
    str += '</hinhanh>';

    str += '<tieudetin>';
    str += form.tieude.value;
    str += '</tieudetin>';

    str += '<tenloaitin>';
    str += form.loaitin.options[form.loaitin.selectedIndex].text;
    str += '</tenloaitin>';

    str += '<nguoidangtin>';
    str += '</nguoidangtin>';

    str += '<ngaydangtin>';
    str += '</ngaydangtin>';

    str += '<lancuoicapnhat>';
    str += '</lancuoicapnhat>';

    str += '<soluotxem>';
    str += '0';
    str += '</soluotxem>';

    str += '<isActive>';
    str += 'True';
    str += '</isActive>';

    str += '</tindang>';

    //alert(str);
    return str;
}

function checkForSubmitAll(tableId){
    var tempTable = document.getElementById(tableId);
    if (tempTable.rows.length > 1) {
        var tmpStr = "<dstindang>";
        for (var i=0; i<tempTable.rows.length;i++){
            var tempCell = tempTable.rows[i].cells.item(2);
            tmpStr += tempCell.innerText;
        }
        tmpStr += "</dstindang>";
        //alert(tmpStr);
        document.getElementById('dsTinDaLuuXMLString').value = tmpStr;
        return true;
    } else {
        alert("Bạn không có tin nào để submit cả!");
        return false;
    }
}